package com.jmx.yp.struts.system;

import org.apache.log4j.*;
import com.jmx.yp.entity.GroupEmployee;
import com.jmx.yp.exception.ErrorCode;
import com.jmx.yp.exception.HandlerException;
import com.jmx.yp.service.system.ISystemService;
import com.opensymphony.xwork2.ActionContext;
import org.springframework.security.providers.encoding.Md5PasswordEncoder;
/**
 * 
 * 功能描述：
 *			系统管理业务控制
 * @author Jenny
 *
 * Date:2011-2-8 下午03:04:05
 */
public class SystemAction {
	private static Logger log = (Logger)Logger.getLogger(SystemAction.class);
	
	private ISystemService systemService;
	
	private Md5PasswordEncoder md5;
	
	public void setSystemService(ISystemService systemService){
		this.systemService = systemService;
	}
	
	public void setMd5(Md5PasswordEncoder md5){
		this.md5 = md5;
	}
	
	public String initPassword(){
		return "updatePassword";
	}
	
	/**
	 * 修改密码
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String updatePassword(){
		GroupEmployee employee = (GroupEmployee)ActionContext.getContext().getSession().get("user");
		
		try{
			if(!employee.getGrpemp_pwd().equals(md5.encodePassword(oldPassword, employee.getGrpemp_id())))
				throw new HandlerException(ErrorCode.PASSWORD_ERROR);
			
			systemService.updatePassword(employee.getSerialid(), 
					md5.encodePassword(newPassword, employee.getGrpemp_id()));
			
			//重新更新session中的用户密码
			employee.setGrpemp_pwd(md5.encodePassword(newPassword, employee.getGrpemp_id()));
			ActionContext.getContext().getSession().put("user", employee);
			
			results = "success";
		}catch(HandlerException ae){
			results = ae.toString();
		}catch(Exception e){
			log.error("SystemAction updatePassword error: " + e.getMessage());
			e.printStackTrace();
			results = ErrorCode.COMMON_ERROR;
		}

		return "success";
	}
	
	private String oldPassword;
	private String newPassword;
	private String results;

	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public void setResults(String results){
		this.results = results;
	}
	
	public String getResults(){
		return this.results;
	}
	
}
